<?php

namespace app\api;
use think\Request;
use think\exception\ValidateException;
use think\exception\HttpResponseException;
use app\api\common;
use Request\Key;
use think\facade\Request as RequestFacade;
/**
 * @title project 基类
 *
 */
class ControllerBase
{
    public $ERROR="";
    public $CODE=201;
    public $SUCCESS;
    public $REQUEST;
    public static $s_server;
     public $noCheckAuth=[
    	'show_template',
    	'opus.OpusSeriesIntroduce/getList',
    	'banner.Index/getList',
    	'user.login/login',
    	'banner.index/getFind',
    //	'index.login/appLogin'
    ];
   
    public function noCheckParam(){
    	$url=$this->REQUEST->pathinfo();
    	$flag=0;
    	foreach($this->noCheckAuth as $k=>$v){
    		if(stripos($url,$v)===0 || stripos($url,$v)>0){
    			$flag=1;
    			break;
    		}
    	}
    	//如果该URL是在不需要验证当中则跳出验证
    	if($flag==1){
    		return true; 
    	}
    	return false;
    }
    
  	public function checkParam(request $request){
  		//1.把不需要检测登陆的排除出去
  		if(!$this->REQUEST){
  			$this->REQUEST=$request;
  		}
  		show_json('');
  		$checkAuth=$this->noCheckParam();
		if($checkAuth==true) return true;
    	$_key_obj=new Key($request);
    	if(!($data=$_key_obj->checkSign())){
    		echo json_encode(['code'=>$_key_obj->code,'msg'=>$_key_obj->error,'data'=>[]]);exit;
    	}
    	return true;
  	}
  	
  	public function checkSign($_sign,$_decryption){
  		$_key_obj=new Key();
  		unset($_decryption['sign']);
  	//	var_dump(isset($_decryption['sign_no_check']) && $_decryption['sign_no_check']==Request()->url(),$_key_obj->sign($_decryption),$_sign);exit;
  		//$_generate_sign=$_key_obj->sign($_decryption);
  		//免验签方法 同一个方法验签过后 再次调用该方法不进行验签
  	//	var_dump($_decryption);exit;
  		if(isset($_decryption['sign_no_check']) && $_decryption['sign_no_check']==Request()->url())
  		{
  			return true;
  		}
  		if($_key_obj->sign($_decryption)!=$_sign)
  		{
  			$_return_data=[
  			'data'=>false,
  			'msg'=>'验签失败',
  			'code'=>201
  			];
  			echo json_encode($_return_data);exit;
  		}
  		$this->REQUEST->setRoute(['sign_no_check'=>Request()->url()]);
  		return true;
  	}

  	
    public function checkVailDate($_class = '', $_type = '')
    {
        $_param = $this->REQUEST->param();
        try {
            $r = validate($_class)->scene($_type)->check($_param);
            
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.field.error', 1);
            $this->ERROR = $this->ERROR . ',' . $e->getError();
            $this->CODE=201;
            return false;
        }
        return true;
    }

    //存日志
    public function requestLog($function = '', $param = [], $name = '')
    {
        $common = new common();
        return $common->requestLog($function, $param, $name);
    }

    //记录所有CODE和msg
    public function getMessageCode($field, $flag)
    {
        if ($flag == 1) {
            $this->ERROR = config($field . '.msg');
        }
        $this->CODE = config($field . '.code');
        $this->SUCCESS = config($field . '.msg');
    }

    //公用请求方法一些不需要调用第三方的直接返回
    public function commonFunction($function)
    {
        try {
            $data = self::$s_server->$function();
            if (isset($this->REQUEST->param()['no_ajax']) && $this->REQUEST->param()['no_ajax'] == 1) {
                $this->ERROR = self::$s_server->error;
                $this->CODE = self::$s_server->code;
                return $data;
            }
            if ($data === false) {
                if(!isset(self::$s_server->CODE)){
                    self::$s_server->CODE=201;
                }
                return common::return_json_layui($data, self::$s_server->ERROR, self::$s_server->CODE);
            } else {
                if(!isset(self::$s_server->CODE)){
                    self::$s_server->CODE=0;
                }
                return common::return_json_layui($data, self::$s_server->SUCCESS, self::$s_server->CODE);
            }
        } catch (HttpResponseException $e) {
            //程序报错的情况下会出现这种
            var_dump($e);
            exit;
            echo 5555;
            exit;
        }
    }

    function getList(){
        return $this->commonFunction(__function__);
    }
    function getDetail(){
        return $this->commonFunction(__function__);
    }
    function add(){
        return $this->commonFunction(__function__);
    }
    function edit(){
        return $this->commonFunction(__function__);
    }
    function del($id = 0){
        return $this->commonFunction(__function__);
    }
}

?>